import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import '../../common_ui/dialog/dialog_factory.dart';
import '../../common_ui/loading.dart';
import '../../repository/api.dart';
import '../../route/route_utils.dart';
import '../../sample/configure/constants.dart';
import '../../utils/persistent_storage.dart';
import '../auth/login_page.dart';

/// 注销账号页面
class UnsubscribePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _UnsubscribekeyPage();
  }
}

Future<bool> _onWillPop() async {
  // PersonalPageState.globalKeythere.currentState?.reloadData();
  return true; // 返回true表示允许返回，返回false表示阻止返回
}

class _UnsubscribekeyPage extends State<UnsubscribePage> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        backgroundColor: Color(0xFFfafafa),
        appBar: _buildTitleBar(context),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(30.0), // 设置内边距
            child: Center(
              // 将 Column 包裹在 Center 中
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start, // 垂直居中
                children: [
                  Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '尊敬的用户:',
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                  Text(
                    '很遗憾，无法继续为您提供服务。注销账号后，您的账号将无法恢复，我们将删除您账号的个人数据，请谨慎操作。',
                    style: TextStyle(
                      fontSize: 16,
                    ),
                  ),
                  SizedBox(height: 20),
                  Text(
                    '注销账号前，我们需要对您信息进行审核以保证您的账号安全，请保证您的账号状态如下:',
                    style: TextStyle(
                      fontSize: 16,
                    ),
                  ),
                  SizedBox(height: 5),
                  Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '1、确保账号没有绑定设备。',
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                  Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '2、账号为正常状态。',
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ),
                  Spacer(),
                  _buildclosenameButton(),
                  SizedBox(height: 40),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  AppBar _buildTitleBar(BuildContext context) {
    return AppBar(
      toolbarHeight: 60,
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios), // 使用没有横线的返回箭头图标
        onPressed: () {
          _onWillPop();
          // 返回操作
          Navigator.of(context).pop();
        },
      ),
      backgroundColor: Color(0xFFffffff),
      automaticallyImplyLeading: false,
      // 禁用自动生成的返回按钮
      title: Text(
        "注销账号",
        style: TextStyle(
          fontSize: 17,
          fontFamily: "PingFang SC",
          fontWeight: FontWeight.w600,
        ),
      ),
      centerTitle: true,
    );
  }

  // 注销账号
  Widget _buildclosenameButton() {
    return SizedBox(
      width: 270, // 设置按钮宽度为400
      height: 45, // 设置按钮高度为40
      child: ElevatedButton(
        onPressed: () {
          DialogFactory.instance.showNeedCloseuserDialog(
            context: context,
            dismissClick: () {
              //是否显示更新红点
              // viewModel.shouldShowUpdateDot();
            },
            confirmClick: () async {
              Loading.showLoading();
              // final resp = await AccountService.delete();
              // if (!resp.success) {
              //   SnackTips.show(context, '注销失败： [${resp.code}] ${resp.message}');
              //   return;
              // }
              bool swtichun = await Api.instance.unsubscribe();
              if (swtichun) {
                await SharedPreferencesUtil.instance
                    .setString(SampleConstants.storageKeyLoginToken, "");
                SharedPreferencesUtil.instance
                    .setString(SampleConstants.storageKeyLoginAccount, "");
                Loading.dismissAll();
                showToast("注销账号成功");
                RouteUtils.pushAndRemoveUntil(context, LoginPage());
              }
            },
          );
        },
        style: ElevatedButton.styleFrom(
          backgroundColor: Color(0xFFececec),
          foregroundColor: Color(0xFFececec),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 80),
          // 调整水平内边距
          elevation: 0,
          shadowColor: Colors.transparent,
        ),
        child: const Text(
          '注 销',
          style: TextStyle(fontSize: 16, color: Color(0xFFfc3d49)),
          textAlign: TextAlign.center, // 确保文字居中
        ),
      ),
    );
  }
}
